//
//  NameViewController.m
//  Ringminder
//
//  Created by Think Small Studio on 09-6-20.
//  Copyright 2009 Think Small Studio. All rights reserved.
//

#import "NameViewController.h"


@implementation NameViewController
@synthesize nameTextField;
@synthesize editingItem;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Set the title of this view
	self.title = @"Set Name";
	
	// Set the view background to match the grouped tables in the other views.
	self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
	
	// Set the right button Save
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
																							target:self action:@selector(save:)] autorelease];
}

// Call this function whenever the view will appear
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	// Initialize the text edit control by setting the properties
	nameTextField.text = [editingItem objectForKey:@"name"];
	nameTextField.font = [UIFont systemFontOfSize:16.0];
	nameTextField.clearButtonMode = UITextFieldViewModeAlways;
	
	[nameTextField becomeFirstResponder];
}

// Call this function whenever the view will disappear
- (void)viewWillDisappear:(BOOL)animated {
    // hides the keyboard
    [nameTextField resignFirstResponder];
}

// Call this function when you hit the Save button
- (IBAction)save:(id)sender {
	// Set the name attribute
	[editingItem setValue:nameTextField.text forKey:@"name"];
	
	// Pop view to return to the main list
    [self.navigationController popViewControllerAnimated:YES];
}

// Call this function when the content in the text box is being changed
- (IBAction)nameChanged:(id)sender {
	self.navigationItem.rightBarButtonItem.enabled = (nameTextField.text.length > 0);
}

// Call this function when you hit the Done button in the keyboard
- (IBAction)textFieldDoneEditing:(id)sender {
	[sender resignFirstResponder];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[nameTextField release];
	[editingItem release];
    [super dealloc];
}


@end
